<template>
  <div class="editor-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-10 offset-md-1 col-xs-12">
          <ul class="error-messages">
            <template v-for="(messages, filed) in errors">
              <li v-for="(message,index) in messages" :key="index">{{ filed }} {{ message }}</li>
            </template>
          </ul>
          <form>
            <fieldset>
              <fieldset class="form-group">
                <input
                  type="text"
                  class="form-control form-control-lg"
                  placeholder="Article Title"
                  v-model="article.title"
                  required
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  type="text"
                  class="form-control"
                  placeholder="What's this article about?"
                  v-model="article.description"
                  required
                />
              </fieldset>
              <fieldset class="form-group">
                <textarea
                  class="form-control"
                  rows="8"
                  placeholder="Write your article (in markdown)"
                  v-model="article.body"
                  required
                ></textarea>
              </fieldset>
              <fieldset class="form-group">
                <input
                  type="text"
                  class="form-control"
                  placeholder="Enter tags"
                  v-model="tag"
                  @keyup.enter="setTags"
                />
                <div class="tag-list">
                  <span class="tag-default tag-pill" v-for="item in article.tagList" :key="item">
                    <i class="ion-close-round"></i>
                    {{item}}
                  </span>
                </div>
              </fieldset>
              <button
                class="btn btn-lg pull-xs-right btn-primary"
                type="button"
                @click="onSubmit"
              >Publish Article</button>
            </fieldset>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getArticle, updateArticles, addArticles } from "@/api/article";
export default {
  //在路由匹配组件渲染之前会先执行中间件处理
  middleware: "authenticated",
  name: "Edit",
  data() {
    return {
      article: {
        title: "",
        description: "",
        body: "",
        tagList: [],
      },
      tag: "",
      slug:this.$route.query.slug,
      errors: {}, //错误信息
    };
  },
  methods: {
    setTags() {
      this.article.tagList.push(this.tag);
      this.tag = "";
    },
    async onSubmit() {
      this.errors = {};
      try {
        const article = this.article;
        if(this.slug){
          await updateArticles(this.slug,article)
        }else{
            await addArticles(article);
        }
       
        this.$router.push({
          name: "article",
          params: {
            slug: article.slug,
          },
        });
      } catch (error) {
        this.errors = error.response.data.errors;
      }
    },
  },
  async created() {
    if (this.slug) {
      const { data } = await getArticle(this.slug);
      this.article = data.article;
    }
  }
};
</script>